(function(app){
	app
		.controller("content_27_inputCtrl",["$scope","$state","myFactory","$http","$stateParams","$state","$q","$ionicHistory",function($scope,$state,myFactory,$http,$stateParams,$state,$q,$ionicHistory){
			//参数定义、接收
			var itemId = $stateParams.changeTypeId;//变更项目ID
			var chdrnum01 =  $stateParams.chdrnum01; //保单号
			var chdrcoy = $stateParams.chdrcoy; //保单公司号码
			var policyData;//保单详情
			var itemData;//用于接收初始化查询initContentItem的内容，里面存在各种各样的内容，比如item02_1
			var searchParam = $stateParams.searchParam;
			var initFlag = false;//检测标志，在初始化函数完成后，会被置为true
			
			//绑定参数到页面
			$scope.chdrnum01 = chdrnum01;
			$scope.declarationFlag = false;//声明标志，默认为false
			$scope.readFlag = false;//阅读协议标志，默认为false
			var token=sessionStorage.getItem("token");
			//S2019051508554《反洗钱监管-官微、移动保全增加核实投被保险关系功能需求》 add by csf 20190606 start
			var isPolicyValue;//是否存在未领取的生存金和年金
			//S2019051508554《反洗钱监管-官微、移动保全增加核实投被保险关系功能需求》 add by csf 20190606 end
			
			//重新受理
			$scope.renew = function(){
				myFactory.renew();
			}	
			//退出移动保全
			$scope.exit = function(){
				myFactory.exit();
			}
			//返回上一页
			$scope.goBack = function(){
				myFactory.goBack();
			}
			//保单详情弹框
			$scope.showPolicyDetail = function(){
				modal('open','policyModalOn27Input','overwindowOn27Input','policyCloseXOn27Input');
				var timestamp=myFactory.getTimetamp();
				var sfis= "chdrnum-chdrcoy-token-timestamp";
				var signStr="chdrnum="+chdrnum01+"&chdrcoy="+chdrcoy+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化时候查询保单详情
				$http({
					method: 'GET',
					url: app.domain + 'query/restFindPolicyDetail.action',
					params: {'chdrnum': chdrnum01,'chdrcoy': chdrcoy,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						policyData = response.data.data;
						$scope.policyData = policyData;
						$scope.policyData.ptdate01=myFactory.DateTools($scope.policyData.ptdate01);
					}
				}, function errorCallback(response) {
					myFactory.alert("","初始化查询订单详情失败!","info");
				});
			}
			//保单详情弹窗关闭函数
			$scope.policyCancelOn27Input = function(){
				modal('close','policyModalOn27Input','overwindowOn27Input','policyCloseXOn27Input');
			}
//			//退保原因弹框
//			$scope.reasonChose = function(){
//				modal('open','reason_model','overwindowOn27Input');
//			}
//			$scope.reasonClose = function(){
//				modal('close','reason_model','overwindowOn27Input');
//			}
			//客户须知弹框
			$scope.tipOpen = function(){
				modal('open','tip_model','overwindowOn27Input');
			}
			$scope.tipClose = function(){
				modal('close','tip_model','overwindowOn27Input');
			}
			//页面同意阅读协议复选框部分
			$scope.changeReadFlag = function(){
				$scope.readFlag = !$scope.readFlag;
				console.log($scope.readFlag);
			}
			//改变下拉框
			$scope.changeReason = function(){
				var options = "";
				options =$("#reasonSelect option:selected");
//				console.log(options.text());
//				console.log(options.val());
//				for(var i= 0; i<=2 ;i++){
//					$scope[reasonInput+i]
//				}

				$scope.reasonInput = options.text();
			}
			//S2019051508554《反洗钱监管-官微、移动保全增加核实投被保险关系功能需求》 add by csf 20190527 start
			//改变投保人和被保人关系的下拉框
			$scope.changePeoRelation = function(){
				var options = $('#reasonPeoRelationSelect option:selected');
				$('#reasonPeoRelationInput').val(options.text());
			} 
			//S2019051508554《反洗钱监管-官微、移动保全增加核实投被保险关系功能需求》 add by csf 20190527 end
			//每次点击多选保证框则反转
			$scope.changeCheckBox = function(){
				$scope.declarationFlag = !$scope.declarationFlag;
				if($scope.declarationFlag==true){
					$scope.declaration = "1";
				}else{
					$scope.declaration = "0";
				}
			}
			
/**********************************************初始化函数begin****************************************************/
			//初始化查询变更项内容
			$scope.load = function () {
				myFactory.loadingShow();//显示加载圈
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-token-timestamp";
				var signStr="itemId="+itemId+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				$http({
					method: 'GET',
					url: app.domain + 'input/initContentItem.action',
					params: {'itemId': itemId,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						itemData = response.data.data.modelMap;
						var item27 = itemData.item27;
						//S2019051508554《反洗钱监管-官微、移动保全增加核实投被保险关系功能需求》 add by csf 20190606 start
						isPolicyValue = itemData.isPolicyValue;
						if ( isPolicyValue == "Y" ){
							$('#show').show();
						}
						//S2019051508554《反洗钱监管-官微、移动保全增加核实投被保险关系功能需求》 add by csf 20190606 end
						$scope.declaration = item27.declaration;
						$scope.reasonSelect = item27.reason;
						$("#reasonSelect").val($scope.reasonSelect);//设置为缓存中的选项
						$scope.changeReason();//执行改变下拉框方法
						if($scope.declaration==0){
							$("#declarationFlag").removeAttr("checked");
						}else if($scope.declaration==1){
							$("#declarationFlag").attr("checked","checked");
						}
						initFlag=true;//标识初始化成功
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","初始化查询变更项内容失败!","info");
				});
			}
/**********************************************初始化函数end****************************************************/
			//关闭遮罩
			$scope.overwindowClose = function(){
				//$(".overwindow").show();
				$(".overwindow").hide();
				$(".overmodal").hide();
			}
			
			$scope.nextStep = function(){
				//判断是否初始化完成了，如果失败，不予进入下一步
				if(!initFlag){
					myFactory.alert("","初始化时失败，请返回上一步重试","info");
					return;
				}
				//读取页面参数
				var reasonInput = $("#reasonInput").val();
				/*S2022013021184监管类-银行令〔2022〕1号营运相关系统功能需求 add by huanghaokun 20220505 start*/
				var reason = $("#reasonSelect option:selected").val();
				if(reason==null || reason==""){
					myFactory.alert("","请选择退保原因","info");
					return;
				}
				/*S2022013021184监管类-银行令〔2022〕1号营运相关系统功能需求 add by huanghaokun 20220505 end*/
				//S2019051508554《反洗钱监管-官微、移动保全增加核实投被保险关系功能需求》 add by csf 20190527 start
				if( isPolicyValue == "Y" ){//存在
					var resonPeoRelationInput = $('#reasonPeoRelationInput');
					if ( resonPeoRelationInput.val() == '' ||resonPeoRelationInput.val() == undefined ){ 
						myFactory.alert("","请选择投被保人关系","info");
						return;
					}
					var resonPeoRelation = $('#reasonPeoRelationSelect');
				
					if (resonPeoRelation.val() == '6' ){
						myFactory.alert("","投保人与被保险人非直系亲属关系，需由被保险人的监护人提出一同申请，请通过书面办理。","info");
						return;
					}
				}
				//S2019051508554《反洗钱监管-官微、移动保全增加核实投被保险关系功能需求》 add by csf 20190527 end
				var readFlag = $scope.readFlag;
				if(readFlag==false){
					myFactory.alert("","请阅读《申请须知》","info");
					return;
				}
				
				var declaration = $scope.declaration; //承诺标志
				var options = "";
				options =$("#reasonSelect option:selected");
				var reason = options.val(); //选择原因
				console.log(declaration);
				console.log(reason);
				
				var item={
					"reason":reason,
					"declaration":declaration,
					//S2019051508554《反洗钱监管-官微、移动保全增加核实投被保险关系功能需求》 add by csf 20190527 start
					"itemId":itemId,
					"peoRelation":$('#reasonPeoRelationSelect').val()
					//S2019051508554《反洗钱监管-官微、移动保全增加核实投被保险关系功能需求》 add by csf 20190527 end
				}
				
				$ionicHistory.clearCache().then(function(){ 
					$state.go('content_check_main',{
						itemId:itemId,
						item:item,
						chdrnum01:chdrnum01,
						chdrcoy:chdrcoy,
						searchParam:searchParam
					});
				})
				
			}
			
		}]);     
		//指针弹窗
		app.directive('infoid',function(){
			return {
				restrict: 'EA',		//指明指令在DOM里面以什么形式被声明,EA.表示即可以是元素也可以是属性
				templateUrl: 'html/input/content_base_info.html'+app.clearCache,
				replace: true,		//设置解析后，DOM声明的指令是否消失
				link: function(scope,element,attrs){  //链接函数负责注册DOM事件和更新DOM,
					console.log('指令完成');
				}
			}
		});
})(app)